#include "cmd2.h"

int pwdFunc(MYSQL* mysql,const int cur_dir_id,char *virtualPath,char *error_buf)
{
    if(cur_dir_id<=0)
    {
        /* printf("id is not legal\n"); */
        strcpy(error_buf,"id is not legal!");
        return -1;
    }

    //通过当前文件所在目录的id找路径
    char sql1[200]={0};
    int id=cur_dir_id;
    sprintf(sql1,"select path,type from vft where id=%d;",id);
    int qret=mysql_query(mysql,sql1);
    if(qret!=0)
    {
        sprintf(error_buf,"%s:%s","mysql_query",mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *rows=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(rows);
    //如果表里不存在符合条件的一行
    if(mysql_num_rows(rows)==0)
    {
        strcpy(error_buf,"vft table don't exit the meeting criteria rows!");
        /* printf("vft table don't exit the meeting criteria rows!\n"); */
        return -1;
    }
    //如果没被删除，且是目录，就正常返回0
    char ch[]="d";
    if(!strcmp(ch,row[2]))
    {
        strcpy(error_buf,"The file corresponding to the ID is not a directory !");
        /* printf("The file corresponding to the ID is not a directory !\n"); */
        return -1; 
    }
    /* printf("%s\n",row[0]); */
    /* strcpy(virtualPath,row[0]); */
    strcpy(virtualPath,row[0]);
    printf("pwd success!\n");
    return 0;
}

int mkdirFunc(MYSQL* mysql,const int cur_dir_id,const char* dirname,const char *username,char *error_buf)
{
    //id不合法
    if(cur_dir_id<=0)
    {
        strcpy(error_buf,"id is not legal");
        return -1;
    }

    //连接mysql的服务端

    //1.先判断是否有相同目录
    char sql[500]={0};
    sprintf(sql,"select filename from vft where parent_id=%d;",cur_dir_id);
    int qret=mysql_query(mysql,sql);
    if(qret!=0)
    {
        sprintf(error_buf,"%s:%s","mysql_query",mysql_error(mysql));
        return -1;
    }
    MYSQL_RES* rows=mysql_store_result(mysql);
    for(unsigned i=0;i<mysql_num_rows(rows);++i)
    {
        MYSQL_ROW row=mysql_fetch_row(rows);
        for(unsigned j=0;j<mysql_num_fields(rows);++j){
            //遍历同一目录下的所有文件
            if(strcmp(row[j],dirname)==0)
            {
                //说明有相同的目录
                strcpy(error_buf,"exit same dir!");
                /* printf("exit same dir!\n"); */
                return -1;
            }
        }
    }
    //判断是文件还是目录
    sprintf(sql,"select path,type from vft where id=%d;",cur_dir_id);
    qret=mysql_query(mysql,sql);
    if(qret!=0)
    {
        sprintf(error_buf,"%s:%s","mysql_query",mysql_error(mysql));
        return -1;
    }

    rows=mysql_store_result(mysql);
    MYSQL_ROW  row=mysql_fetch_row(rows);
    if(mysql_num_rows(rows)==0)
    {
        strcpy(error_buf,"vft table don't exit the meeting criterial rows");
        /* printf("vft table don't exit the meeting criterial rows\n"); */
        return -1;
    }
    //判断是不是文件类型
    if(strcmp(row[1],"d"))
    {
        strcpy(error_buf,"The file corresponding to id isn't a dir!");
        /* printf("The file corresponding to id isn't a dir!\n"); */
        return -1;
    }

    //将新目录的path合起来
    char new_path[200]={0};
    strcat(new_path,row[0]);
    strcat(new_path,"/");
    strcat(new_path,dirname);

    //3.插入新的一行
    bzero(sql,sizeof(sql));
    sprintf(sql,"insert into vft (filename,user,parent_id,type,path) values('%s','%s','%d','d','%s');",dirname,username,cur_dir_id,new_path);
    /* printf("mkdir sql=%s\n",sql); */
    qret=mysql_query(mysql,sql);
    if(qret!=0)
    {
        sprintf(error_buf,"%s:%s","mysql_query",mysql_error(mysql));
        return -1;
    }
    printf("mkdir success!\n");

    return 0;
}

//1.判断存不存在
//2.判断是文件还是目录
//3.判断目录是否为空
//4.删除空目录
int rmdirFunc(MYSQL* mysql,const int cur_dir_id,const char* dirname,char *error_buf)
{

    char sql[500]={0};

    sprintf(sql,"select id,type from vft where parent_id=%d and filename='%s'; ",cur_dir_id,dirname);
    /* printf("sql=%s\n",sql); */
    int qret=mysql_query(mysql,sql);
    if(qret!=0)
    {
        sprintf(error_buf,"%s:%s","mysql_query",mysql_error(mysql));
        return -1;
    }

    MYSQL_RES* rows=mysql_store_result(mysql);
    if(mysql_num_rows(rows)==0)
    {
        //不存在
        strcpy(error_buf,"not exit dirname!");
        /* printf("not exit dirname!\n"); */
        return -1;
    }
    //存在，判断是file还是dir
    for(unsigned i=0;i<mysql_num_rows(rows);++i)
    {
        MYSQL_ROW row=mysql_fetch_row(rows);
        if(strcmp("d",row[1]))
        {
            //不是目录
            strcpy(error_buf,"is not dir!");
            /* printf("is not dir!\n"); */
            return -1;
        }
        else
        {
            //是目录
            int id=atoi(row[0]);
            bzero(sql,sizeof(sql));
            sprintf(sql,"select id from vft where parent_id=%d;",id);
            /* printf("sql=%s\n",sql); */
            qret=mysql_query(mysql,sql);
            if(qret!=0)
            {
                sprintf(error_buf,"%s:%s","mysql_query",mysql_error(mysql));
                return -1;
            }
            MYSQL_RES *rows1=mysql_store_result(mysql);
           /* printf("%ld\n",mysql_num_rows(rows1)); */
            if(mysql_num_rows(rows1)>0)
            {
                //目录不空
                strcpy(error_buf,"dir is not empty!");
                /* printf("dir is not empty!\n"); */
                return -1;
            }
            else
            {
                //目录为空
                /* printf("3\n"); */
                bzero(sql,sizeof(sql));
                sprintf(sql,"delete from vft where id=%d;",id);
                /* printf("rmdir sql=%s\n",sql); */
                /* printf("sql=%s\n",sql); */
                qret=mysql_query(mysql,sql);
                /* printf("4\n"); */
                if(qret!=0)
                {
                    sprintf(error_buf,"%s:%s","mysql_query",mysql_error(mysql));
                    return -1;
                }
                printf("rmdir success!\n");
            }
        }
    }
    /* printf("\n"); */
    return 0;
}


int insertFile(MYSQL *mysql,const char *filename,const int cur_dir_id,const char* md5,const int filesize,char *error_buf)
{
    char sql[4096]={0};

    //1.先判断有无重名文件
    sprintf(sql,"select filename from vft where parent_id=%d and type='f'and filename='%s';",cur_dir_id,filename);
    int qret=mysql_query(mysql,sql);
    printf("sql=%s\n",sql);
    if(qret!=0)
    {
        sprintf(error_buf,"%s:%s","mysql_query",mysql_error(mysql));
        return -1;
    }
    MYSQL_RES* rows=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(rows);
    if(mysql_num_rows(rows)!=0)
    {
        strcpy(error_buf,"exit same name file!\n");
        return -1;
    }

    //无重名文件,找当前目录的信息
    sprintf(sql,"select user,path from vft where id=%d;",cur_dir_id);
    /* printf("sql=%s\n",sql); */
    qret=mysql_query(mysql,sql);
    if(qret!=0)
    {
        sprintf(error_buf,"%s:%s","mysql_query",mysql_error(mysql));
        return -1;
    }
    rows=mysql_store_result(mysql);
    row=mysql_fetch_row(rows);

    //row[0]是user,row[1]是目录的path
    char file_path[500]={0};
    strcpy(file_path,row[1]);
    /* printf("file_path=%s\n",file_path); */
    strcat(file_path,"/");
    strcat(file_path,filename);
    /* printf("file_path=%s\n",file_path); */
    bzero(sql,sizeof(sql));
    sprintf(sql,"INSERT INTO vft (filename,user,parent_id,md5,type,path,size)VALUES('%s','%s',%d,'%s','f','%s',%d);",filename,row[0],cur_dir_id,md5,file_path,filesize);
    qret=mysql_query(mysql,sql);
    /* printf("sql=%s\n",sql); */
    if(qret!=0)
    {
        sprintf(error_buf,"%s:%s","mysql_query",mysql_error(mysql));
        return -1;
    }

    return 0;
}

